const path = require('path');
// const ExtractTextPlugin = require('extract-text-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const vuxLoader = require('vux-loader');

// @link webpack4.0参照文档https://mp.weixin.qq.com/s/bkV8XscUWjxUWjXeVk-8Ng；以及官方文档

const webpackConfig = {
	entry: {
		main: './src/main',
	},
	output: {
		path: path.join(__dirname, '../../public/wx-system/dist'),
	},
	module: {
		rules: [
			{
				test: /\.vue$/,
				loader: 'vue-loader',
				options: {
					loaders: {
						js: 'happypack/loader?id=babel',
					},
				},
				include: [
					path.resolve(__dirname, 'src'),
					path.resolve(__dirname, 'node_modules'),
				],
			},
			{
				test: /\.js$/,
				loader: 'happypack/loader?id=babel',
				exclude: /node_modules/,
				include: path.resolve(__dirname, 'src'),
			},
			{
				test: /\.css$/,
				// use: ExtractTextPlugin.extract({
				// 	use: ['css-loader?minimize', 'postcss-loader'],
				// 	fallback: 'style-loader',
				// }),
				use: [
					MiniCssExtractPlugin.loader,
					'css-loader',
					'postcss-loader',
				],
				include: [
					path.resolve(__dirname, 'src'),
					path.resolve(__dirname, './node_modules/cube-ui/lib/'),
				],
				// exclude: /node_modules/,
			},
			{
				test: /\.less/,
				// use: ExtractTextPlugin.extract({
				// 	use: ['postcss-loader', 'less-loader'],
				// 	fallback: 'style-loader',
				// }),
				use: [
					MiniCssExtractPlugin.loader,
					'css-loader',
					'postcss-loader',
					'less-loader',
				],
				exclude: /node_modules/,
				include: path.resolve(__dirname, 'src'),
			},
			{
				test: /\.styl$/,
				// use: ExtractTextPlugin.extract({
				// 	use: ['postcss-loader', 'stylus-loader'],
				// 	fallback: 'style-loader',
				// }),
				use: [
					MiniCssExtractPlugin.loader,
					'postcss-loader',
					'stylus-loader',
					'style-loader',
				],
				include: [
					path.resolve(__dirname, 'src'),
					path.resolve(__dirname, './node_modules/cube-ui/src/'),
				],
			},
			{
				test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
				loader: 'url-loader?limit=1024',
				include: [
					path.resolve(__dirname, 'src'),
					path.resolve(__dirname, './node_modules/cube-ui/lib/'),
				],
			},
			{
				test: /\.(html|tpl)$/,
				loader: 'html-loader',
				exclude: /node_modules/,
			},
			{
				test: /\.(swf|ttf|eot|svg|woff(2))(\?[a-z0-9]+)?$/,
				loader: 'file-loader',
				exclude: /node_modules/,
				include: [
					path.resolve(__dirname, 'src'),
				],
			},
		],
	},
	resolve: {
		extensions: ['.js', '.vue', '.png'],
		alias: {
			vue: 'vue/dist/vue.esm.js',
			src: path.resolve(__dirname, './src'),
		},
	},
};

const vuxConfig = vuxLoader.merge(webpackConfig, {
	plugins: ['vux-ui'],
});

module.exports = vuxConfig;
